<?php
/**
 * Created by PhpStorm.
 * User: Monkey
 * Date: 2021/10/13
 * Time: 11:27
 * Desc: 产品回收站
 */
declare (strict_types=1);

namespace app\backend\controller;

use app\backend\base\controller\Base;

use app\backend\validate\RecycleValidate;
use app\extend\model\ProductData;
use think\facade\Db;

class Recycle extends Base
{
    /**
     * 列表
     */
    public function index()
    {
        if (request()->isPost()) {
            $validate = new RecycleValidate();
            $ret = $validate->scene('index')->check(input());
            if (!$ret) {
                return json(_E($validate->getError()));
            }

            $page = input('page/d', 1);
            $limit = input('limit/d', 20);
            $fieldName = input('field_name', '');
            $fieldValue = input('field_value', '');
            $cateId = input('cateId/d', '');

            $where = [
                ['is_delete', '=', 1] //已删除
            ];

            //字段筛选
            if (!empty($fieldName) && !empty($fieldValue)) {
                switch ($fieldName) {
                    case 'product_name'://产品名称
                        $where[] = ['product_name', '=', $fieldValue];
                        break;
                }
            }

            //产品分类
            if (is_numeric($cateId)) {
                $where[] = ['cate_id', '=', $cateId];
            }

            $list = [];
            $count = \app\extend\model\Product::where($where)->count();
            if ($count > 0) {
                $list = \app\extend\model\Product::where($where)->append(['cate_name_txt'])->order(['sort' => 'asc', 'id' => 'asc'])->page($page)->limit($limit)->select();
            }

            return json([
                'code' => 0,
                'msg' => '获取成功',
                'count' => $count,
                'data' => $list,
            ])->send();
        } else {
            $cateList = \app\extend\model\Cate::field('id,cate_name')->order(['id' => 'asc'])->select();
            return view('product/recycle', [
                'cate_list' => $cateList
            ]);
        }
    }

    /**
     * 还原
     */
    public function restore()
    {
        if(request()->isPost()){
            $validate = new RecycleValidate();
            $ret = $validate->scene('restore')->check(input());
            if(!$ret){
                return json(_E($validate->getError()));
            }

            $id = input('id/d',0);

            try {
                // 启动事务
                Db::startTrans();

                \app\extend\model\Product::where([
                    ['id','=',$id]
                ])->update([
                    'is_delete' => 0,
                    'updated_at' => date('Y-m-d H:i:s')
                ]);

                ProductData::where([
                    ['product_id','=',$id]
                ])->update([
                    'is_delete' => 0,
                    'updated_at' => date('Y-m-d H:i:s')
                ]);

                Db::commit();
                return json(_S('还原成功'));
            } catch (\Exception $e) {
                Db::rollback();
                return json(_E('还原失败,错误:'.$e->getMessage()));
            }
        }
    }
}
